package org.univie.mappr;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.maps.GeoPoint;

/**
 * Represents a Flickr photo object that can be used throughout the application
 */
public class FlickrPhoto {
	/**
	 * A 75x75px thumbnail
	 */
	public static final int SIZE_SMALL = 0;
	/**
	 * Maximum of 500px on the longest side
	 */
	public static final int SIZE_MEDIUM = 1;
	/**
	 *  Maximum of 1024px on the longest side
	 */
	public static final int SIZE_LARGE = 2;
	/**
	 * A 75x75px thumbnail
	 */
	public static final int SIZE_THUMB = 3;

	private String mId;
	private int mFarmId;
	private int mServerId;
	private String mSecret;

	private String mOwnerNsid;
	private String mOwnerUserName;
	private String mOwnerRealName;

	private String mUrl;
	private String mTitle;
	private String mDesc;

	private Date mDatePosted;
	private Date mDateTaken;

	private GeoPoint mLocation;

	private String[] mTags;

	/**
	 * Creates a new photo and stores the thumbnail locally
	 * 
	 * @param Id
	 *            The ID of the photo
	 * @param farmId
	 *            The ID of Flickr's farm
	 * @param serverId
	 *            The ID of Flickr's server
	 * @param secret
	 *            The secret of the photo
	 */
	public FlickrPhoto(String Id, int farmId, int serverId, String secret) {
		mId = Id;
		mFarmId = farmId;
		mServerId = serverId;
		mSecret = secret;
		mUrl = "http://farm" + farmId + ".static.flickr.com/" + serverId + "/"
				+ Id + "_" + secret;
		
		// immediately add the thumbnail
		if (!ThumbnailCache.hasThumbnail(this))
			ThumbnailCache.addThumbnail(this);
	}

	/**
	 * Adds information about the photo's owner
	 * 
	 * @param string
	 *            The NSID of the owner
	 * @param userName
	 *            The owner's user name on Flickr
	 * @param realName
	 *            The owner's real name
	 */
	public void setOwner(String ownerNsid, String userName, String realName) {
		mOwnerNsid = ownerNsid;
		mOwnerUserName = userName;
		mOwnerRealName = realName;
	}

	/**
	 * Returns the URI for the photo for a given size
	 * 
	 * @param size
	 *            The size of the photo (SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE, SIZE_THUMB)
	 * @return The URI for the photo in the given size
	 */
	public Uri getUri(int size) {
		Uri.Builder builder = new Uri.Builder();

		switch (size) {
		case SIZE_SMALL:
			builder.appendPath(mUrl + "_s.jpg");
			return builder.build();
		case SIZE_MEDIUM:
			builder.appendPath(mUrl + ".jpg");
			return builder.build();
		case SIZE_LARGE:
			builder.appendPath(mUrl + "_z.jpg");
			return builder.build();
		case SIZE_THUMB:
			builder.appendPath(mUrl + "_s.jpg");
			return builder.build();
		default:
			builder.appendPath(mUrl + ".jpg");
			return builder.build();
		}
	}
	
	public URL getUrl(int size) {
		try {
			switch (size) {
			case SIZE_SMALL:
				return new URL(mUrl + "_s.jpg");
			case SIZE_MEDIUM:
				return new URL(mUrl + ".jpg");
			case SIZE_LARGE:
				return new URL(mUrl + "_z.jpg");
			case SIZE_THUMB:
				return new URL(mUrl + "_s.jpg");
			default:
				return new URL(mUrl + ".jpg");
			}		
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	// Other getters and setters follow:

	public String getTitle() {
		return mTitle;
	}

	public void setTitle(String mTitle) {
		this.mTitle = mTitle;
	}

	public String getDesc() {
		return mDesc;
	}

	public void setDesc(String mDesc) {
		this.mDesc = mDesc;
	}

	public Date getDatePosted() {
		return mDatePosted;
	}

	public void setDatePosted(Date mDatePosted) {
		this.mDatePosted = mDatePosted;
	}

	public Date getDateTaken() {
		return mDateTaken;
	}

	public void setDateTaken(Date mDateTaken) {
		this.mDateTaken = mDateTaken;
	}

	public Drawable getThumbnailLocal() {
		return ThumbnailCache.getDrawable(this);
	}

	public Drawable getPhotoLocal() {
		return FullscreenCache.getDrawable(this);
	}

	public String getId() {
		return mId;	
	}
	
	public String[] getTags() {
		return mTags;
	}

	public void setTags(String[] mTags) {
		this.mTags = mTags;
	}

	public String getOwnerNsid() {
		return mOwnerNsid;
	}

	public String getOwnerRealName() {
		return mOwnerRealName;
	}

	public String getOwnerUserName() {
		return mOwnerUserName;
	}

	public GeoPoint getLocation() {
		return mLocation;
	}

	public void setLocation(GeoPoint location) {
		this.mLocation = location;
	}
}
